package com.maserb.monitor.service.schoolConduct.impl;

import com.maserb.monitor.entity.schoolConduct.ApplicationRecommendEntity;
import com.maserb.monitor.entity.schoolConduct.StudentConductRecordEntity;
import com.maserb.monitor.entity.schoolStudent.SchoolStudentEntity;
import com.maserb.monitor.repository.schoolConduct.ApplicationRecommendRepository;
import com.maserb.monitor.repository.schoolConduct.StudentConductRecordRepository;
import com.maserb.monitor.repository.schoolStudent.SchoolStudentRepository;
import com.maserb.monitor.service.base.impl.JpaSpecificationServiceImpl;
import com.maserb.monitor.service.schoolConduct.ApplicationRecommendService;
import com.maserb.monitor.service.schoolConduct.StudentConductRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

@Service
public class ApplicationRecommendServiceImpl extends JpaSpecificationServiceImpl<ApplicationRecommendRepository,
        ApplicationRecommendRepository, ApplicationRecommendEntity, String>
        implements ApplicationRecommendService {


    @Override
    public Page<ApplicationRecommendEntity> findAll(Pageable pageable, String name) {
        return this.findAll(new Specification<ApplicationRecommendEntity>() {
            @Override
            public Predicate toPredicate(Root<ApplicationRecommendEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<String>pathName = root.get("name");
                ArrayList<Predicate> list = new ArrayList<Predicate>();
                if (!StringUtils.isEmptyOrWhitespace(name)) {
                    list.add(criteriaBuilder.and(criteriaBuilder.like(pathName, "%"+name+"%")));
                }
                return criteriaBuilder.and(list.toArray(new Predicate[list.size()]));
            }
        },pageable);
    }
}